package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.PmFaults;
import com.ruoyi.system.domain.PmRepairOrders;
import com.ruoyi.system.domain.PmResources;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IPmFaultsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.Workorders;
import com.ruoyi.system.service.IWorkordersService;

import javax.servlet.ServletException;

/**
 * 工单记录，用于跟踪故障的维修工作进度Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-23
 */
@Service
public class WorkordersServiceImpl implements IWorkordersService {
    @Autowired
    private WorkordersMapper workordersMapper;
    @Autowired
    private PmFaultsMapper pmFaultsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private PmRepairOrdersMapper pmRepairOrdersMapper;

    /**
     * 查询工单记录，用于跟踪故障的维修工作进度
     *
     * @param id 工单记录，用于跟踪故障的维修工作进度主键
     * @return 工单记录，用于跟踪故障的维修工作进度
     */
    @Override
    public Workorders selectWorkordersById(Long id) {
        return workordersMapper.selectWorkordersById(id);
    }

    /**
     * 查询工单记录，用于跟踪故障的维修工作进度列表
     *
     * @param workorders 工单记录，用于跟踪故障的维修工作进度
     * @return 工单记录，用于跟踪故障的维修工作进度
     */
    @Override
    public List<Workorders> selectWorkordersList(Workorders workorders) {
        return workordersMapper.selectWorkordersList(workorders);
    }

    /**
     * 新增工单记录，用于跟踪故障的维修工作进度
     *
     * @param workorders 工单记录，用于跟踪故障的维修工作进度
     * @return 结果
     */
    @Override
    public int insertWorkorders(Workorders workorders) {
        if (ObjectUtils.isEmpty(workorders.getFaultId())) {
            throw new ServiceException("故障信息不能为空");
        }
        if (ObjectUtils.isEmpty(workorders.getTechnicianId())) {
            throw new ServiceException("维修人员不能为空");
        }
        SysUser sysUser = sysUserMapper.selectUserById(workorders.getTechnicianId());
        if (ObjectUtils.isEmpty(sysUser)) {
            throw new ServiceException("维修人员查询错误");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        PmFaults pmFaults = pmFaultsMapper.selectPmFaultsByFaultId(workorders.getFaultId());
        workorders.setCreatorId(loginUser.getUserId());
        workorders.setCreatorName(loginUser.getUsername());
        workorders.setFaultMac(pmFaults.getDeviceId());
        workorders.setTechnicianName(sysUser.getUserName());
        pmFaults.setStatus("3");
        workorders.setStatus("3");
        pmFaultsMapper.updatePmFaults(pmFaults);
        workordersMapper.updateWorkorders(workorders);
        pmFaultsMapper.updatePmFaults(pmFaults);
        PmRepairOrders pmRepairOrders = new PmRepairOrders();
        BeanUtils.copyProperties(pmFaults, pmRepairOrders);
        pmRepairOrders.setCreatorId(sysUser.getUserId());
        pmRepairOrders.setCreatorName(sysUser.getUserName());
        pmRepairOrders.setWorkCreatorName(workorders.getCreatorName());
        pmRepairOrders.setRepairDate(new Date());
        int i = workordersMapper.insertWorkorders(workorders);
        pmRepairOrders.setWorkOrderId(workorders.getId());
        pmRepairOrdersMapper.insertPmRepairOrders(pmRepairOrders);
        return i;
    }

    /**
     * 修改工单记录，用于跟踪故障的维修工作进度
     *
     * @param workorders 工单记录，用于跟踪故障的维修工作进度
     * @return 结果
     */
    @Override
    public int updateWorkorders(Workorders workorders) {
        return workordersMapper.updateWorkorders(workorders);
    }

    /**
     * 批量删除工单记录，用于跟踪故障的维修工作进度
     *
     * @param ids 需要删除的工单记录，用于跟踪故障的维修工作进度主键
     * @return 结果
     */
    @Override
    public int deleteWorkordersByIds(Long[] ids) {
        return workordersMapper.deleteWorkordersByIds(ids);
    }

    /**
     * 删除工单记录，用于跟踪故障的维修工作进度信息
     *
     * @param id 工单记录，用于跟踪故障的维修工作进度主键
     * @return 结果
     */
    @Override
    public int deleteWorkordersById(Long id) {
        return workordersMapper.deleteWorkordersById(id);
    }
}
